package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.CdkeyDto;
import org.linlinjava.litemall.admin.dto.ChargeDto;
import org.linlinjava.litemall.admin.service.AdminChargeService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallCompanyCharge;
import org.linlinjava.litemall.db.service.LitemallCompanyChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/charge")
@Validated
public class AdminChargeController {
    private final Log logger = LogFactory.getLog(AdminChargeController.class);

    @Autowired
    private LitemallCompanyChargeService chargeService;
    @Autowired
    private AdminChargeService adminChargeService;

    @RequiresPermissions("admin:charge:list")
    @RequiresPermissionsDesc(menu = {"企业管理", "充值管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name, String content,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallCompanyCharge> chargeList = chargeService.querySelective(name, content, page, limit, sort, order);
        return ResponseUtil.okList(chargeList);
    }

    private Object validate(ChargeDto charge) {

        return null;
    }

    @RequiresPermissions("admin:charge:create")
    @RequiresPermissionsDesc(menu = {"企业管理", "充值管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody ChargeDto charge) {
        Object error = adminChargeService.add(charge);
        if(error == null) {
            return ResponseUtil.ok(charge);
        }else{
            return error;
        }
    }

    @RequiresPermissions("admin:charge:read")
    @RequiresPermissionsDesc(menu = {"企业管理", "充值管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        LitemallCompanyCharge charge = chargeService.findById(id);
        return ResponseUtil.ok(charge);
    }

    @RequiresPermissions("admin:charge:delete")
    @RequiresPermissionsDesc(menu = {"企业管理", "充值管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallCompanyCharge charge) {
        Integer id = charge.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        chargeService.deleteById(id);
        return ResponseUtil.ok();
    }

}
